﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfumeShopManagement.BLL;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.Utility;

namespace PerfumeShopManagement.View
{
    public partial class frmAccountManagement : Form
    {
        private BllAccount bllAccount = new BllAccount();

        public frmAccountManagement()
        {
            InitializeComponent();
        }

        private void resetId()
        {
            txbUsername.Text = "";
        }

        private void resetFieldWithoutId()
        {
            // Reset without id
            txbPassword.Text = "";
            txbCustomerName.Text = "";
            txbPhoneNumber.Text = "";
            txbAddress.Text = "";
        }

        private void reloadGridView()
        {
            List<User> accounts = bllAccount.GetAll();

            // Load grid view
            object[] buffer = new object[2];
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            foreach (User item in accounts)
            {
                buffer[0] = item.Username;
                buffer[1] = item.Password;
                buffer[2] = item.Name;
                buffer[3] = item.PhoneNumber;
                buffer[4] = item.Address;

                rows.Add(new DataGridViewRow());
                rows[rows.Count - 1].CreateCells(gridAccounts, buffer);
            }
            gridAccounts.Rows.Clear();
            gridAccounts.Rows.AddRange(rows.ToArray());
        }

        private void btnAddAccount_Click(object sender, EventArgs e)
        {
            resetId();
            resetFieldWithoutId();
        }

        private void btnDeleteAccount_Click(object sender, EventArgs e)
        {
            if (gridAccounts.SelectedRows.Count > 0)
            {
                bool allDeleted = true;
                foreach (DataGridViewRow item in gridAccounts.SelectedRows)
                {
                    string id = (string)item.Cells["colAccountId"].Value;
                    if (bllAccount.DeleteById(id))
                        allDeleted = false;
                }

                if (allDeleted)
                {
                    MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
                }
                else
                {
                    MessageBox.Show(Constants.FAILED_MESSAGE);
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            resetFieldWithoutId();
        }

        private void btnSaveAccount_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.Username = txbUsername.Text;
            user.Password = txbPassword.Text;
            user.RoleId = Constants.USER_ROLE;

            if (bllAccount.Save(user))
            {
                reloadGridView();
                MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
            }
            else
            {
                MessageBox.Show(Constants.FAILED_MESSAGE);
            }
        }

        private void frmAccountManagement_Load(object sender, EventArgs e)
        {
            resetId();
            resetFieldWithoutId();
            reloadGridView();
        }
    }
}
